-- ========================================================
-- @File	: controller.lua
-- @Brief	: 控制器基类
-- @Author	: Leo Zhao
-- @Date	: 2016-05-05
-- ========================================================
controller = controller or {}

-- 声明一个controller
function controller.new()
	return inherit(controller);
end

-- 控制器的初始化函数
function controller.init()
end

-- 输出html
function controller.html(template, data)
	local render = require('core.template');
	header("Content-type:text/html");
	render.render('./app/views/' .. template, data);
end

-- 输出json
function controller.json(data)
	local encoder = require('core.json');
	header("Content-type:application/json");
	echo(encoder.encode(data));
end

-- 输出文件(下载)
function controller.file(name, content, len)
	header('Content-length:' .. (len or string.len(content)));
	header('Content-type:application/octet-stream')
	header('Content-disposition:attachment; filename="' .. name .. '"');
	echo(content);
end

-- 倒计时跳转
function controller.jump(url, title, message, wait)
	local render = require('core.template');
	header("Content-type:text/html");
	render.render('./public/jumper.html', {
		title = title, 
		url = url, 
		message = message, 
		wait = wait});
	_REDIRECT = true;
	error('redirect');
end
